package com.jbh.wanandroidkotlin.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Color.*
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.TreeResult
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.util.dipToPx
import kotlinx.android.synthetic.main.fragment_t_x.*
import android.widget.LinearLayout.LayoutParams as LayoutParams

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"


class TXFragment : Fragment() {

    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_t_x, container, false)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String="", param2: String="") =
            TXFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }




    @SuppressLint("ResourceAsColor")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        titleView = TabLayout(activity!!)
            .apply {
                background = getDrawableBg(Color.WHITE, dipToPx(activity!!,50).toFloat())
                tabMode = TabLayout.MODE_FIXED
                setTabTextColors(Color.BLACK,Color.RED)
                setSelectedTabIndicator(getDrawableBg(BLUE,100f))
                setSelectedTabIndicatorColor(android.R.color.transparent)
            }

        mActionBarView
            .apply {
                addTitleView(titleView!!,LayoutParams( dipToPx(activity!!,120),dipToPx(activity!!,30)))
            }

        initData()
    }





    private var titleView :TabLayout ?=null
    private val title = listOf<String>("体系","导航")
    private val list  = mutableListOf<Fragment>()


    private fun getDrawableBg(color:Int,r:Float):GradientDrawable = GradientDrawable().apply {
        setColor(color)
        cornerRadius = r
    }


    private fun initData() {
        list.add(ItemTXFragment.newInstance("1"))
        list.add(ItemTXFragment.newInstance("2"))
        mViewPager.adapter = TXPageAdapter(activity!!.supportFragmentManager)
        titleView?.setupWithViewPager(mViewPager)
    }



    inner class TXPageAdapter(fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager) {
        override fun getCount(): Int {
            return list.size
        }
        override fun getItem(position: Int): Fragment {
            return list[position]
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return title[position]
        }
    }

}